package com.fuqi.android.zhongcai.business.shuig.view.activity;

import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.bean.VisitBean;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.business.shuig.request.api.CreateVisitApi;
import com.fuqi.android.zhongcai.business.shuig.request.api.VisitApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.CreateVisitResult;
import com.fuqi.android.zhongcai.business.shuig.request.result.VisitResult;
import com.fuqi.android.zhongcai.business.shuig.view.adapter.AnswerAdapter;
import com.fuqi.android.zhongcai.business.shuig.view.adapter.AnswerQuestionAdapter;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.RegexUtil;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.google.gson.JsonArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.fuqi.framework.gson.GsonHelper.writeValue;

public class ShuiGHuiFangActivity extends BaseActivity {
    @Bind(R.id.gv_visitType)
    GridView mGvVisitType;
    @Bind(R.id.gv_visitContent)
    GridView mGvVisitContent;
    @Bind(R.id.lv_questionList)
    ListView mLvQuestion;
    @Bind(R.id.et_remark)
    EditText mEtRemark;

    AnswerAdapter mTypeAdapter,mContentAdapter;
    AnswerQuestionAdapter mQuestionAdapter;
    List<VisitBean> typeData = new ArrayList<VisitBean>();
    List<VisitBean> contentData = new ArrayList<VisitBean>();
    List<VisitBean> questionData = new ArrayList<VisitBean>();

    String sVisitContent,sVisitType,sQuestionList;

    ARResponse response;
    @OnClick({R.id.okadvice})
    public void onClickListener(View view) {

        switch (view.getId()) {
            case R.id.okadvice:
                SparseBooleanArray poss = mGvVisitType.getCheckedItemPositions();
                for (int i = 0; i < poss.size(); i++) {
                    int key = poss.keyAt(i);
                    if(poss.get(key)){
                        sVisitType = typeData.get(key).getBaseCode();
                    }
                }
                poss = mGvVisitContent.getCheckedItemPositions();
                for (int i = 0; i < poss.size(); i++) {
                    int key = poss.keyAt(i);
                    if(poss.get(key)){
                        sVisitContent = contentData.get(key).getBaseCode() ;
                    }
                }
                JsonArray jsonArray = mQuestionAdapter.getItem();

                sQuestionList = jsonArray.toString();
                if(!RegexUtil.checkNotNull(sVisitType)) {
                    ActivityUtils.toast("请完成问卷再提交");
                    break;
                }else if(!RegexUtil.checkNotNull(sVisitContent)) {
                    ActivityUtils.toast("请完成问卷再提交");
                    break;
                }else if(jsonArray.size() < questionData.size()) {
                    ActivityUtils.toast("请完成问卷再提交");
                    break;
                }
                RequestManager.getInstance().call(new CreateVisitApi(
                        new CreateVisitApi.CreateVistParams(response.getValue("ORDER_ID"),mEtRemark.getText().toString(),
                                sVisitContent,sVisitType,sQuestionList),
                        respfListener, errorListener));
                break;
        }
    }

    Response.Listener<CreateVisitResult> respfListener = new Response.Listener<CreateVisitResult>() {

        @Override
        public void onResponse(CreateVisitResult response) {
            if (HandleCode.requestSuccess()) {
                ActivityUtils.toast("回访完成。");
                finish();
                Log.i("juner", "juner OrderDetailResult response : " + writeValue(response));
            } else {
            }
        }
    };

    Response.Listener<VisitResult> respListener = new Response.Listener<VisitResult>() {

        @Override
        public void onResponse(VisitResult response) {
            if (HandleCode.requestSuccess()) {
                typeData = response.getVisitTypes();
                contentData = response.getVisitContents();
                questionData = response.getQuestions();

                mTypeAdapter.setDate(typeData);
                mContentAdapter.setDate(contentData);
                mQuestionAdapter.setDate(questionData);
                Log.i("juner", "juner OrderDetailResult response : " + writeValue(response));
            } else {
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public void initView() {
    }
    @Override
    public void initListener() {
        mGvVisitType.setOnItemClickListener(new GridView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
            }
        });
    }

    @Override
    public void initData() {
        titleBar.setTitle("业主回访");
        response = new ARResponse(getIntent().getStringExtra("response"));

        /*typeData.add(new VisitBean("","电话回访","","",""));
        typeData.add(new VisitBean("","现场回访","","",""));
        typeData.add(new VisitBean("","其他","","",""));
        contentData.add(new VisitBean("","产品安装情况","","",""));
        contentData.add(new VisitBean("","产品问题反馈","","",""));
        contentData.add(new VisitBean("","公司服务相应情况","","",""));
        contentData.add(new VisitBean("","其他","","",""));

        VisitBean que1 = new VisitBean("","工人上门安装是否按照约定的时间1","","","");
        VisitBean que2 = new VisitBean("","工人上门安装是否按照约定的时间2","","","");
        VisitBean que3 = new VisitBean("","工人上门安装是否按照约定的时间3","","","");
        VisitBean que4 = new VisitBean("","工人上门安装是否按照约定的时间4","","","");
        que1.setRls(typeData);
        que2.setRls(contentData);
        que3.setRls(typeData);
        que4.setRls(contentData);
        questionData.add(que1);
        questionData.add(que2);
        questionData.add(que3);
        questionData.add(que4);*/

        mTypeAdapter = new AnswerAdapter(this, typeData);
        mContentAdapter = new AnswerAdapter(this, contentData);
        mQuestionAdapter = new AnswerQuestionAdapter(this, questionData);

        RequestManager.getInstance().call(new VisitApi(
                new VisitApi.VisitParams(), respListener, errorListener));

//        mTypeAdapter.setDate(typeData);

        mGvVisitType.setAdapter(mTypeAdapter);
        mGvVisitContent.setAdapter(mContentAdapter);
        mLvQuestion.setAdapter(mQuestionAdapter);
    }

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_huifang;
    }


}
